// const MongoClient = require('mongodb').MongoClient;
const {MongoClient, ObjectID} = require('mongodb');

let obj = new ObjectID();

console.log(obj);
// MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
//     if (err) {
//         console.log('Unable to connect to MongoDB server');
//     }
//
//     console.log('Connected to MongoDB server')
//
//     // mongodb driver3 需要改为以下格式
//     db.db('TodoApp').collection('Todos').insertOne({
//         text: 'Something to do',
//         completed: false
//     }, (err, result) => {
//         if (err) {
//             return console.log('Unable to insert todo', err)
//         }
//
//         console.log(JSON.stringify(result.ops, undefined, 2));
//
//     })
//
//     db.close();
// });


MongoClient.connect('mongodb://localhost:27017/TodaApp', (err, db) => {
    if (err) {
        return console.log('Unable to connect the mongodb server')
    }

    console.log('Connected to MongDB server');

    // db.db('TodoApp').collection('Todos').find({
    //     _id: new ObjectID('5a38b03d1a71aad3e03b0260')
    // }).toArray().then((docs) => {
    //     console.log('Todos: ')
    //     console.log(JSON.stringify(docs, undefined, 2))
    // },(err) => {
    //     console.log(err)
    // })

    // db.db('TodoApp').collection('Todos').find().count().then((count) => {
    //     console.log(`Todos count: ${count}`)
    // },(err) => {
    //     console.log(err)
    // })



    db.db('TodoApp').collection('Users').find({name: 'Tony'}).count().then((count) => {
        console.log(`Users count: ${count}`)
    },(err) => {
        console.log(err)
    })

    db.db('TodoApp').collection('Users').find({name: 'Tony'}).toArray().then((docs) => {
        console.log(JSON.stringify(docs, undefined, 2));
    })





    // db.close();

})

// Connection URL
// var url = 'mongodb://localhost:27017/myproject';
//
// var insertDocuments = function(db, callback) {
//     // Get the documents collection
//     var collection = db.collection('documents');
//     // Insert some documents
//     collection.insertMany([
//         {a : 1}, {a : 2}, {a : 3}
//     ], function(err, result) {
//         assert.equal(err, null);
//         assert.equal(3, result.result.n);
//         assert.equal(3, result.ops.length);
//         console.log("Inserted 3 documents into the collection");
//         callback(result);
//     });
// }
//
// // Use connect method to connect to the server
// // Use connect method to connect to the server
// MongoClient.connect(url, function(err, db) {
//     assert.equal(null, err);
//     console.log("Connected successfully to server");
//
//     insertDocuments(db, function() {
//         db.close();
//     });
// });
